package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.AcmMethod;
import com.example.demo.entity.JobCodeHigh;
import com.example.demo.entity.result.MethodRatio;
import com.example.demo.entity.result.RatioNum;
import com.example.demo.mapper.AcmMethodMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/acmMethod")
public class AcmMethodController {

    @Resource
    AcmMethodMapper acmMethodMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody AcmMethod acmMethod) {
        acmMethodMapper.insert(acmMethod);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        acmMethodMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "4000") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<AcmMethod> wrapper = Wrappers.<AcmMethod>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(AcmMethod::getMethod,search);
        }
        Page<AcmMethod> acmMethodPage = acmMethodMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<AcmMethod> records = acmMethodPage.getRecords();

        Collections.sort(records);
        Collections.reverse(records);

        List<MethodRatio> mr10 = new ArrayList<>();
//        System.out.println(mr10);
        Map<String,Double> mp = new HashMap(); // key:方法->value:总通过率
        Map<String,Integer> cnt = new HashMap(); // key:方法->value:题数

        // 判断知识点时候容易掌握的方法，选该方法所有题目通过率求平均值，题目数量需大于32。

        for(int i=0;i<records.size();i++) {
            String method=records.get(i).getMethod();
            Double ratio=records.get(i).getPassRatio();
            if(!cnt.containsKey(method)) {
                mp.put(method,0.0);
                cnt.put(method,0);
            }
            mp.put(method,mp.get(method)+ratio);  // 计算该种方法的总通过率
            cnt.put(method,cnt.get(method)+1);   // 计算该种方法的总题数
        }


        for(Map.Entry<String, Double> entry : mp.entrySet()){
                Double avgRatio=entry.getValue()*1.0/cnt.get(entry.getKey());
                mp.put(entry.getKey(),avgRatio);
        }

        for(Map.Entry<String, Double> entry : mp.entrySet()){
//            System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
            Integer num=cnt.get(entry.getKey());
            if(num>32) {
                mr10.add(new MethodRatio(entry.getKey(),entry.getValue(),num));
            }
        }

        Collections.sort(mr10);
        Collections.reverse(mr10);

        System.out.println(mr10.size());

        Page<MethodRatio> methodRatioPage = new Page();  // 创建MethodRatio类型的Page,前端需要该类型的数据
        methodRatioPage.setRecords(mr10);
        methodRatioPage.setTotal(acmMethodPage.getTotal());
        methodRatioPage.setSize(acmMethodPage.getSize());
        methodRatioPage.setCurrent(acmMethodPage.getCurrent());
        methodRatioPage.setOrders(acmMethodPage.getOrders());
        methodRatioPage.setPages(acmMethodPage.getPages());

        return Result.success(methodRatioPage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody AcmMethod acmMethod) {
        acmMethodMapper.updateById(acmMethod);
        return Result.success();
    }
}
